package com.lys.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.lys.commonutils.R;
import com.lys.eduservice.cilent.VodClient;
import com.lys.eduservice.entity.EduVideo;
import com.lys.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Lyyyys
 * @since 2021-04-30
 */
@Api(description="小节管理")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient client;

    @ApiOperation(value = "获取小节信息")
    @GetMapping("getVideo/{id}")
    public R getVideoById(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video",video);
    }

    @ApiOperation(value = "添加小节信息")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        boolean save = eduVideoService.save(eduVideo);
        return save == true ? R.ok() : R.error();
    }

    @ApiOperation(value = "修改小节信息")
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        boolean flag = eduVideoService.updateById(eduVideo);
        return flag == true ? R.ok() :R.error();
    }


    //TODO后面这个方法需要完善：删除小节的时候，同时把里面视频删除
    //一定要先删视频再删小节不然小节id删掉以后视频id查不到了
    @ApiOperation(value = "删除小节信息")
    @DeleteMapping("{id}")
    public R deleteVideoById(@PathVariable String id){
        //此时的id是小节id，要得到的是视频id，将视频id传入
        //所以根据小节id获取视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoId = eduVideo.getVideoSourceId();
        //先判断小节中是否有视频id
        if(!StringUtils.isEmpty(videoId)){
            client.removeAlyVideo(videoId);
        }
        boolean flag = eduVideoService.removeById(id);
        return flag == true ? R.ok():R.error();
    }
}

